<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">

<head th:replace="/common/include :: header(~{::title}, ~{::link}, ~{::style})">
	<title>工具管理-代码生成</title>
</head>
<body class="gray-bg">
    <div class="container-div">
		<div class="row">
			<th:block th:include="/common/breadcrumb :: breadcrumb"/>
			<div class="col-sm-12 search-collapse">
				<form id="search-form">
					<div class="select-list row">
						<div class="col-sm-3 col-xs-6">
							<div class="control-label">表单名称：</div>
							<div><input class="form-control" type="text" name="Q_tableName_S_LK"/></div>
						</div>
						<div class="col-sm-3 col-xs-6">
							<div class="control-label">表单描述：</div>
							<div><input class="form-control" type="text" name="Q_tableComment_S_LK"/></div>
						</div>
						<div class="right-btn">
							<a class="btn btn-success btn-rounded btn-sm" onclick="$.table.search('bootstrap-table', 'search-form')"><i class="fa fa-search"></i>&nbsp;搜索</a>
							<a class="btn btn-default btn-rounded btn-sm" onclick="$.form.reset('search-form')"><i class="fa fa-refresh"></i>&nbsp;重置</a>
						</div>
					</div>
				</form>
			</div>

            <div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="initTable()" th:classappend="${@config.hasPermission('tool:gen:init')}">
					<i class="fa fa-download"></i> 初始化表单
				</a>
				<a class="btn btn-primary btn-del disabled" onclick="gen()" th:classappend="${@config.hasPermission('tool:gen:edit')}">
					<i class="fa fa-download"></i> 批量生成代码
				</a>
	        </div>

			<div class="col-sm-12 select-table">
				<table id="bootstrap-table" class="table-striped"></table>
			</div>
	    </div>
	</div>

	<div th:replace="/common/include :: footer"></div>

	<script th:inline="javascript">
		var editFlag = [[${@config.hasPermission('tool:gen:edit')}]];
		var prefix = ctx + 'api/system/generator';

		$(function() {
            var options = {
                code: 'tableId',
                url: prefix + '/table',
                sortName: 'tableName',
                sortOrder: 'asc',
                search: false,
                showExport: false,
                pagination: true,
                columns: [{
						field: 'rowState',
                    	checkbox: true
                	},
                    {
                        field: 'tableName',
                        title: '表名称',
                        sortable: true
                    },
                    {
                        field: 'className',
                        title: '类名称'
                    },
                    {
                        field: 'tableComment',
                        title: '表描述'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        width: '80px',
                        formatter: function(value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn-xs ' + editFlag + '" href="#" onclick="edit(\'' + row.tableId + '\',\'' + row.tableName + '\')"><i class="fa fa-edit"></i>配置元数据规则</a> ');
                            actions.push('<a class="btn-xs ' + editFlag + '" href="#" onclick="genJava(\'' + row.tableId + '\',\'' + row.tableName + '\')"><i class="fa fa-bug"></i>生成Java代码</a> ');
                            actions.push('<a class="btn-xs ' + editFlag + '" href="#" onclick="genHtml(\'' + row.tableId + '\',\'' + row.tableName + '\')"><i class="fa fa-bug"></i>生成Html代码</a> ');
                            actions.push('<a class="btn-xs ' + editFlag + '" href="#" onclick="gen(\'\',\'' + row.tableId + '\',\'' + row.tableName + '\')"><i class="fa fa-bug"></i>生成所有代码</a> ');
                            return $.common.dropdown(actions.join(''));
                        }
                    }]
            };
            $.table.init('bootstrap-table', options);
		});

		function initTable() {
			// 表单赋值
			$.operate.post({
				url: prefix + '/init',
				success: function(result) {
					if (result.code === 200) {
						refresh();
					} else {
						$.modal.toast.error(result.msg, result.param);
					}
				},
				error: function(result) {
					$.modal.toast.error(result.msg, result.param);
				}
			});
		}

		// 配置规则
        function edit(tableId, tableName) {
            var url = ctx + 'api/tool/gen/edit';
            url = url + '?id=' + tableId + '&table=' + tableName;
            $.operate.editFull({
                title: '配置[' + tableName + ']字段属性',
                content: url,
                shadeClose: true,
                _index: window.name
            });
        }

        // 生成代码
		function gen(url, tableId, tableName) {
            if (!tableId) {
                var rows = $('#' + $.table._table_id).bootstrapTable('getSelections');
                if ($.common.isEmpty(rows)) {
					$.modal.toast.warning('请至少选择一条记录');
                    return;
                }
                tableId = '';
                var key = $.table._option.code;
                $.each(rows, function(index, row){
                    tableId += row[key] + ',';
                });
                tableId = tableId.substring(0, tableId.length - 1);
            }

            if (!url) {
                url = '/genCode?tableId=' + tableId;
			}

            $.modal.lay.confirm('确定要生成[' + tableName + ']表代码吗？', function() {
                top.location.href = prefix + url;
				$.modal.toast.info('正在生成代码请稍后…');
            })
		}

        function genJava(tableId, tableName) {
            var url = '/genCode?tableId=' + tableId + '&type=java';
            gen(url, tableId, tableName);
        }

        function genHtml(tableId, tableName) {
            var url = '/genCode?tableId=' + tableId + '&type=html';
            gen(url, tableId, tableName);
        }

		function refresh() {
		    $.table.refresh();
		}
	</script>
</body>
</html>